package shooting.ui.unit
{
	import fl.motion.easing.Linear;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import shooting.event.UnitEvents;
	import shooting.motion.tween.PointTweener;
	import shooting.ui.MissileContainer;
	import shooting.ui.missilepattern.IMissilePattern;
	import shooting.ui.missilepattern.MissalePatternNwayBase;
	import shooting.ui.unit.waypack.WayPackSet0;

	public class UnitBase extends EventDispatcher implements IUnit
	{
		private var _kind:String;
		private var _HP:Number;
		private var _startPosition:Point;
		private var _position:Point;
		private var _direction:Number;
		private var _wayPack:Array;
		private var _msContainer:MissileContainer;
		private var tweener:PointTweener;
		private var offsetPoint:Point;
		
		private var renderCount:uint = 0;
		private var patternIdx:uint;
		public var easingFunc:Function = Linear.easeIn;
		
		private var msPattern:IMissilePattern;
		private var msPattern2:IMissilePattern;
		
		public function UnitBase(kind:String, startPosition:Point, wayPack:Array = null, HP:Number = 100, direction:Number =0, msContainer:MissileContainer = null)
		{
			_kind = kind;
			_startPosition = startPosition;
			_HP = HP;
			_direction = direction;
			_position = _startPosition.clone();
			_msContainer = msContainer;
			if(wayPack == null)
			{
				wayPack = WayPackSet0.GOFRONT;
			}
			_wayPack = wayPack;
			
			tweener = new PointTweener();
			tweener.addEventListener( Event.COMPLETE, nextPattern);
			
			patternIdx = 0;
			renderCount = 0;
			
			setPointTweener();
			
			//=========test
			setMsPattern();
		}

		public function render():void
		{
			renderCount++;
			offsetPoint = tweener.tweenStep();
			
			//_position = offsetPoint;
			
			_position.x = offsetPoint.x;
			_position.y = offsetPoint.y;
			//trace(offsetPoint.y, _position.y);
			//msPattern.render();
			msPattern2.render();
		}
		
		public function getPosition():Point
		{
			return _position;
		}
		
		public function get kind():String
		{
			return _kind;
		}
		
		public function get dirction():Number
		{
			//return _direction;
			return Math.PI;
		}
		
		private function setPointTweener():void
		{
			var positionObj:Object = _wayPack[patternIdx];
			tweener.setTtween(_position.clone(), _startPosition.add(positionObj.position), positionObj.delay, easingFunc);
		}
		private function nextPattern(e:Event):void
		{
			patternIdx++;
			if(patternIdx>=_wayPack.length)
			{
				this.dispatchEvent(new UnitEvents(UnitEvents.UNIT_REMOVE, this) );
				return;
			}
			setPointTweener();
		}
		
		private function setMsPattern():void
		{
			msPattern = new MissalePatternNwayBase("smallBlue", _position, 0, 3, 20, 0, 3, 15, 4, 3);
			msPattern2 = new MissalePatternNwayBase("smallRed", _position, 0, 25, 1, 0, .003, 40, 3, 6);
			msPattern.setContainer(_msContainer);
			msPattern2.setContainer(_msContainer);
		}
	}
}